import 'package:flutter_shop/helpers/HttpProxy.dart';
import 'package:flutter_shop/helpers/dataModels.dart';

const servicePath = {
  "homePageContent": 'wxmini/homePageContent',
  "homePageBelowConten": 'wxmini/homePageBelowConten',
  "category": "wxmini/getCategory",
  "getMallGoods": "wxmini/getMallGoods",
  "getGoodDetailById": "wxmini/getGoodDetailById",
};

class Api {
  static Future<HttpBaseResult> get<D>(
      String path, Map<String, Object> urlArgs) async {
    var p = new HttpProxy.get(path, urlArgs);
    return await p.send<D>();
  }

  static Future<HttpBaseResult<D>> post<D>(String path,
      Map<String, Object> urlArgs, Map<String, Object> bodyArgs) async {
    var p = HttpProxy.post(path, urlArgs, bodyArgs);
    // var res = await p.send<D>();
    // return Api.convertMapToDataModel(res.data);
    return await p.send<D>();
  }

  static T convertMapToDataModel<T extends BaseModel>(dynamic data) {
    // TODO: 将要实现将数据对象model 转换为 class model
    String key = T.toString();
    var modelClass = modelMap[key];
    return modelClass;
  }

  static Future<HttpBaseResult<LoginModel>> login() async {
    return await Api.get('/login', {});
  }

  static Future<HttpBaseResult> getCategory() async {
    return await Api.post("wxmini/getCategory", {}, {});
  }

  static Future<HttpBaseResult> getHomePageContent() async {
    return await Api.post("wxmini/homePageContent", {},
        {'lon': '32.162746', 'lat': '118.703763'});
  }

  static Future<HttpBaseResult> getHomePageBelowConten(int page) async {
    return await Api.post("wxmini/homePageBelowConten", {}, {"page": page});
  }

  static Future<HttpBaseResult> getCategoryGoodsList(
      int page, String categoryId, String categorySubId) async {
    return await Api.post("wxmini/getMallGoods", {}, {
      "page": page,
      "categorySubId": categorySubId,
      "categoryId": categoryId
    });
  }
}
